package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;



    @Autowired
    private DishFlavorService dishFlavorService;

    //添加菜品 口味
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveDishWithFlavor(dishDto);
        return R.success("菜品添加成功!");
    }

    //菜品分页展示
    @GetMapping("page")
    public R<Page> page(@RequestParam int page,int pageSize,String name){
        //菜品基本信息分页展示
        Page<Dish> dishPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        lqw.orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage,lqw);

        //菜品加菜品分类分页展示
        Page<DishDto> dishDtoPage=new Page<>();
        List<DishDto> dishDtoList=new ArrayList<>();
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<Dish> records = dishPage.getRecords();
        records.forEach(dish -> {
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            Category category = categoryService.getById(dish.getCategoryId());
            if (category!=null){
                dishDto.setCategoryName(category.getName());
            }
            dishDtoList.add(dishDto);
        });
        dishDtoPage.setRecords(dishDtoList);
        return R.success(dishDtoPage);
    }
    //删除菜品
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        dishService.deleteDishWithFlavor(ids);
        return R.success("菜品删除成功!");
    }
    /**
     * 根据条件返回信息
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){

        //分页查询器
//        Page<Dish> page = new Page<>();
        //构建条件查询器
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        //先通过sort进行升序排列，在根据修改时间进行降序排列
        lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        //根据分类id查询
        lqw.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        //只要已经启售的菜品
        lqw.eq(dish.getStatus() != null, Dish::getStatus,"1");
        //获得数据对象集合
        List<Dish> list = dishService.list(lqw);
        //创建dtoList集合用于放置查询数据
        List<DishDto> dtoList = new ArrayList<>();
        //遍历list集合，操作每一个对象
        list.forEach(dish1 -> {
            //新建DishDto对象
            DishDto dishDto = new DishDto();
            //copy原对象到新对象dishDto中
            BeanUtils.copyProperties(dish1,dishDto);
            //构建条件查询器
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            //根据菜品id查询菜品分类集合
            lambdaQueryWrapper.eq(DishFlavor::getDishId,dish1.getId());
            List<DishFlavor> flavorList = dishFlavorService.list(lambdaQueryWrapper);
            //将flavorList放到dishDto对象当中
            dishDto.setFlavors(flavorList);
            //将dishDto对象放到dtoLista集合中当中
            dtoList.add(dishDto);
        });

        return R.success(dtoList);
    }
    //修改菜品状态
    @PostMapping("/status/{type}")
    public R<String> status(@PathVariable int type,@RequestParam List<Long> ids){
        for (Long id : ids) {
            Dish dish = dishService.getById(id);
            if (type==0){
                dish.setStatus(0);
            }else {
                dish.setStatus(1);
            }
            dishService.updateById(dish);
        }
        return R.success("菜品状态修改成功!");
    }

    //根据id查看菜品
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){
        DishDto dishDto=dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    //修改菜品
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("菜品修改成功!");
    }


}
